package com.yk.framework.gui.wf.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.event.MouseInputListener;

import com.yk.framework.gui.ctrl.YKDialog;
import com.yk.framework.gui.wf.Contants;
import com.yk.framework.gui.wf.ui.bean.ConditionNodeBean;
import com.yk.framework.gui.wf.ui.form.ConditionForm;

/**
 * @author lifetime
 * 
 */
public class ConditionAction implements MouseInputListener {
	WFPanel wfPanel;
	JLabel myLbl;
	Border mouseOnBorder;
	Border oldBorder;
	Border selectBorder = new VBorder();

	public ConditionAction(WFPanel wfPanel, JLabel myLbl) {
		this.wfPanel = wfPanel;
		this.myLbl = myLbl;
		oldBorder = myLbl.getBorder();
		mouseOnBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.gray, Color.gray.brighter().brighter().brighter());
	}

	private static class VBorder implements Border {
		public void paintBorder(Component paramComponent, Graphics paramGraphics, int paramInt1, int paramInt2, int paramInt3, int paramInt4) {
			Graphics2D g2 = (Graphics2D) paramGraphics.create();
			g2.setColor(Color.gray);
			g2.setStroke(WFPanel.vbs);
			Rectangle rec = paramGraphics.getClipBounds();
			g2.drawRoundRect(rec.x, rec.y, rec.width - 1, rec.height - 1, 5, 5);
		}

		public Insets getBorderInsets(Component paramComponent) {
			return new Insets(0, 0, 0, 0);
		}

		public boolean isBorderOpaque() {
			return false;
		}
	}

	public void mouseClicked(MouseEvent e) {
		if (SwingUtilities.isLeftMouseButton(e)) {
			myLbl.setBorder(selectBorder);
			oldBorder = selectBorder;
			myLbl.putClientProperty("isSelected", Boolean.TRUE);
			wfPanel.changeOnlyOneSelected(myLbl);
			if (wfPanel.isWFDesign() && e.getClickCount() == 2) {
				YKDialog dialog = new YKDialog(SwingUtilities.getWindowAncestor(wfPanel));
				dialog.setTitle(Contants.WF_System_PropertiseUITitle);
				dialog.setResizable(false);
				dialog.setModal(true);
				ConditionForm form = new ConditionForm(dialog, myLbl);
				ConditionNodeBean nodeBean = (ConditionNodeBean) myLbl.getClientProperty("editData");
				form.setEditData(nodeBean);
				form.loadFields();
				form.setCancel(true);
				dialog.getContentPane().add(form);
				dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
				dialog.setBounds(new Rectangle(form.getPreferredSize().width, form.getPreferredSize().height + 30));
				dialog.setLocationRelativeTo(null);
				dialog.setVisible(true);
				if (!form.isCancel()) {
					form.storeFields();
				}
				form.setCancel(true);
			}
		}
	}

	public void mousePressed(MouseEvent e) {

	}

	public void mouseReleased(MouseEvent e) {

	}

	public void mouseEntered(MouseEvent e) {
		oldBorder = myLbl.getBorder();
		myLbl.setBorder(mouseOnBorder);
	}

	public void mouseExited(MouseEvent e) {
		myLbl.setBorder(oldBorder);
	}

	public void mouseDragged(MouseEvent e) {

	}

	public void mouseMoved(MouseEvent e) {

	}

}
